package com.mp.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.ibatis.session.RowBounds;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.druid.sql.visitor.functions.Insert;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.mp.entity.Employee;
import com.mp.entity.User;
import com.mp.mapper.EmployeeMapper;
import com.mp.mapper.UserMapper;

import net.sf.jsqlparser.statement.delete.Delete;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-dao.xml")
public class TestMP {
	@Autowired
	private DataSource dataSource;
	
	@Autowired
	private UserMapper userMapper;
	
	@Test
	public void testInsert() {
		User user = new User();
		user.setUsername("贝贝");
		user.setPassword("000");
		userMapper.insert(user);
		System.out.println(user);
	}
	
	@Test
	public void testUpdate() {
		User user = new User();
		user.setId(4);
		user.setUsername("小芳");
		//update根据id进行更新，没传的字段不会更新，即有选择性的更新
		//userMapper.updateById(user);
		//根据id更新，没传的值就更新为null
		userMapper.updateAllColumnById(user);
	}
	
	@Test
	public void testSelect() {
		//根据id查询
		User user = userMapper.selectById(1);
		System.out.println(user);
		//根据条件查询一条数据
		User user2 = new User();
		user2.setId(2);
		user2.setUsername("李思思");
		userMapper.selectOne(user2);
		//查询全部数据,查询全部的就直接传递null
		userMapper.selectByMap(null);
		//根据条件查询多条数据,此时需要接住map封装参数
		Map<String,Object> map = new HashMap<String, Object>();
		//key值为数据库表中的列明，value为条件
		map.put("username", "贝贝");
		List<User> list = userMapper.selectByMap(map);
		System.out.println(list.size());
		user.setUsername("贝贝");
		
		//通过id批量查询
		ArrayList<Integer> list2 = new ArrayList<Integer>();
		list2.add(1);
		list2.add(2);
		List<User> selectBatchIds = userMapper.selectBatchIds(list2);
		System.out.println(selectBatchIds.size());
		
		//分页查询,第二个参数是分页查询的条件，这是一个内存分页
		//List<User> list3 = userMapper.selectPage(new RowBounds(0, 2),null);
		List<User> list3 = userMapper.selectPage(new Page<>(0,3), null);
		System.out.println(list3.toString());
	}
	
	@Test
	public void delete() {
		//根据id进行删除
		  userMapper.deleteById(5); 
		  //根据条件进行删除，需要一个map将条件放进去 
		  Map<String, Object> columnMap = new HashMap<String, Object>(); columnMap.put("username", "贝贝");
		  userMapper.deleteByMap(columnMap);
		 
		//批量删除，需要一个list集合
		List<Integer> idList = new ArrayList<Integer>();
		idList.add(6);
		idList.add(7);
		userMapper.deleteBatchIds(idList);
		
	}
	
	//使用条件构造器EntityWrapper
	//1、分页查询年龄<=20的男性员工,并根据年龄排序
	@Autowired
	private EmployeeMapper employeeMapper;
	@Test
	public void selectPage() {
		//设置分页参数
		RowBounds rowBounds = new RowBounds(0, 2);
		//EntityWrapper设置条件     
		Wrapper<Employee> wrapper = new EntityWrapper<Employee>().eq("sex", "男")
				.lt("age", 21)//小于
				//.orderBy("age")//默认升序
				.orderDesc(Arrays.asList(new String[] {"age"}))//降序
				.like("name", "五") //名字中带有五
				//.or() //或者
				//.between("age", 16, 20) //betwwee 条件语句 
				;
		List<Employee> list = employeeMapper.selectPage(rowBounds , wrapper );
		System.out.println(list.toString());
	} 
	
	//2、根据条件进行修改操作(之前的基本操作中的更新都是根据主键)
	@Test
	public void testEntityWrapperUpdate() {
		//将名字带有六的的性别为女的客户名字修改为志玲姐姐，年龄16
		Employee employee = new Employee();
		employee.setName("志玲姐姐");
		employee.setAge(16);
		System.out.println(employee);
		//实例化一个条件构造器，将条件封装进去
		Wrapper<Employee> wrapper = new EntityWrapper<Employee>()
				.eq("sex", "女")
				.like("name", "六");
		employeeMapper.update(employee, wrapper);
	}
	
	//3根据条件进行删除操作
		@Test
		public void testEntityWrapperDelete() {
			//实例化一个条件构造器，将条件封装进去
			Wrapper<Employee> wrapper = new EntityWrapper<Employee>()
					.eq("sex", "女")
					.like("name", "物");
			employeeMapper.delete(wrapper);
		}
	
	//测试公共字段填充，不设定添加的名字，自动填充
	@Test
	public void testHandlerInsert() {
		Employee employee = new Employee();
		employee.setSex("小美眉");
		employee.setAge(21);
		employee.insert();
	}
		
	
	
	@Test
	public void test() {
		try {
			dataSource.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
}
